package com.cresch.ical.downloader.ical;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 
 * @author Resch Christian
 *
 */
public class ElementSplitter {

	private SplitterInterface splitter;
	private String input;
	
	public ElementSplitter (SplitterInterface splitter) {
		this.splitter = splitter;
	}
	
	public String singleStringElement(String startTag, String endTag) {
		return (this.singleStringElement(startTag, endTag, this.input));
	}
	
	public String singleStringElement(String startTag, String endTag, String input) {
		String stringElement = new String("");
		
		splitter.Initialize(startTag, endTag);
		ArrayList<String> resultList = splitter.splittUp(input);
		if (resultList.size() == 1) {
			stringElement = resultList.get(0);
		}
		
		return stringElement;
	}
	
	public Date singleDateElement(String startTag, String endTag, String dateFormat) throws ParseException {
		return (this.singleDateElement(startTag, endTag, dateFormat, this.input));
	}
	
	public Date singleDateElement(String startTag, String endTag, String dateFormat, String input) throws ParseException {
		Date dateElement = new Date();
		
		splitter.Initialize(startTag, endTag);
		ArrayList<String> resultList = splitter.splittUp(input);
		if (resultList.size() == 1) {
			DateFormat df = new SimpleDateFormat(dateFormat);
			dateElement = df.parse(resultList.get(0));
		}
		
		return dateElement;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}
}
